﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using MageWars.Support.StateMachines.Behaviors;
using MageWars.GameElements;

namespace MageWars.Support.StateMachines.States
{
    public class SeekSM : State
    {
        public SeekSM(Mage Self, Vector2 Destination)
            : base(Self)
        {
            CurrentBehavior = new Seek(Self, Destination);
        }

        public SeekSM(Mage Self, Vector3 Destination)
            : this(Self, new Vector2(Destination.X, Destination.Y))
        {
        }

        public SeekSM(Mage Self, Mage Target)
            : base(Self)
        {
            this.Target = Target;
            CurrentBehavior = new Seek(Self, new Vector2(Target.Position.X, Target.Position.Y));
        }

        public Mage Target
        {
            get;
            private set;
        }

        public override Node[] GetCamino()
        {
            return CurrentBehavior.GetCamino();
        }

        private Behavior CurrentBehavior { get; set; }

        public override MWAction GetNextAction()
        {
            var result = CurrentBehavior.GetNextAction();
            if (this.Target != null)
            {
                if (this.Target.IsDead)
                {
                    foreach (var m in Mage.Mages)
                    {
                        if (m.Group != Self.Group && !m.IsDead)
                        {
                            return new MWAction() { NewState = new SeekSM(Self, m) };
                        }
                    }
                    return new MWAction() { NewState = new Guard(Self, Self.Position) };
                }
                else
                {

                    if (this.Target.Group != Self.Group)
                    {
                        if (MageMath.GetDistance(this.Target.Position, this.Self.Position) < Attack.Range)
                        {
                            //if (Self.HP > 30)
                            //{
                            //    return new MWAction() { NewState = new Attack(Self) };
                            //}
                            //else 
                            //{
                            //    return new MWAction() { NewState = new Flee(Self) }; 
                            //}
                            return new MWAction() { NewState = new Attack(Self) };
                        }
                        else
                        {
                            return result;
                        }
                    }
                    else
                    {
                        return result;
                    }
                }
            }
            else
            {
                if (result.Finished)
                {
                    return new MWAction() { NewState = new Guard(Self, Self.Position) };
                }
                else
                {
                    return result;
                }
            }

        }

    }
}
